import { Message } from "../../models/message";

import { onlyChangeComplete, whetherLogin } from "../../utils/util";
const app = getApp();
const message = new Message();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: "",
    active: 0,
    systemMsg: [],
    communityMsg: [],
    allDelFlag: 0,
    isRead: 0,
    loading: false,
    noMore: false,
    pageNo: 1,
    loadingFailed: false,
    token: wx.getStorageSync("token"),
    memberIdStor: wx.getStorageSync("user").memberId,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var { baseImgUrl } = getApp().globalImgUrl.config;
    this.setData({
      baseImgUrl,
      pageNo: 1,
      systemMsg: [],
      communityMsg: [],
      allDelFlag: "",
      isRead: "",
      loading: false,
      noMore: false,
      token: wx.getStorageSync("token"),
      memberIdStor: wx.getStorageSync("user").memberId,
    });

    this.getTabBar().init();
    this.initPageData();
    this.getcommunityMsg();
  },
  onlyChangeComplete,
  whetherLogin,
  goDetail(e) {
    this.onlyChangeComplete();
    let { id, navigatetitle } = e.currentTarget.dataset;
    wx.navigateTo({
      url:
        "/owner-app/pages/messge-detail/index?id=" +
        id +
        "&navigatetitle=" +
        navigatetitle,
    });
  },
  initPageData() {
    if (!this.data.token) {
      this.whetherLogin(this.data.token);
      return;
    }

    if (!this.data.memberIdStor) {
      return;
    }

    message
      .getSystemMsg()
      .then((res) => {
        this.setData({
          systemMsg: res.result.messages,
          allDelFlag: res.result.allDelFlag,
        });
      })
      .catch((err) => {
        console.log(err);
      });
  },
  getcommunityMsg() {
    if (!this.data.memberIdStor) {
      return;
    }

    message
      .getCommunityMsg()
      .then((res) => {
        if (res.result.allDelFlag == undefined) {
          this.setData({
            communityMsg: [],
            isRead: 1,
          });
        } else {
          this.setData({
            communityMsg: res.result.messages,
            isRead: res.result.allDelFlag,
          });
        }
      })
      .catch((err) => {
        console.log(err);
      });
  },
  scrollBottom() {
    let active = this.data.active;
    if (!this.data.loading && !this.data.noMore) {
      this.setData({
        loading: true,
        pageNo: this.data.pageNo + 1,
      });
      if (active == 0) {
        this.getData(
          "getSystemMsg",
          "loading",
          "systemMsg",
          "pageNo",
          "loadingFailed",
          0,
          "noMore",
          true
        );
      } else {
        this.getData(
          "getCommunityMsg",
          "loading",
          "communityMsg",
          "pageNo",
          "loadingFailed",
          0,
          "noMore",
          true
        );
      }
    }
  },
  getData(
    methodName,
    loadingName,
    dataName,
    pageNoName,
    loadingFailedName,
    type = 0,
    noMoreName,
    isPage
  ) {
    message[methodName](this.data[pageNoName], 10, type)
      .then((res) => {
        this.setData({
          [loadingName]: false,
        });
        //有分页
        if (isPage) {
          this.setData({
            [dataName]: this.data[dataName].concat(res.result.messages),
          });
        }
        //没有分页就直接赋值
        else {
          this.setData({
            [dataName]: res.records.records,
          });
        }
        if (res.result.messages.length == 0) {
          this.setData({
            [noMoreName]: true,
          });
        }
      })
      .catch((err) => {
        this.setData({
          [loadingFailedName]: true,
        });
      });
  },
  onShareAppMessage: function () {
    return {
      title: "标准化智慧社区", //转发的标题。当前小程序名称
      path: `/pages/home/index`, //转发的路径
      imageUrl: "../../static/icon/share.jpg", //自定义图片路径 支持PNG及JPG。显示图片长宽比是 5:4。
    };
  },
  onChange(v) {
    if (!this.data.token) {
      this.whetherLogin(this.data.token);
      return;
    }

    this.onlyChangeComplete();
    let index = v.detail.index;
    this.setData({
      pageNo: 1,
      active: index,
    });
    this.initPageData();
    this.getcommunityMsg();
  },
});
